﻿
                                  <div class="box border primary">
											<div class="box-title">
												<h4><i class="fa fa-bars"></i>Form controls</h4>
												<div class="tools hidden-xs">
													<a href="#box-config" data-toggle="modal" class="config">
														<i class="fa fa-cog"></i>
													</a>
													<a href="javascript:;" class="reload">
														<i class="fa fa-refresh"></i>
													</a>
													<a href="javascript:;" class="collapse">
														<i class="fa fa-chevron-up"></i>
													</a>
													<a href="javascript:;" class="remove">
														<i class="fa fa-times"></i>
													</a>
												</div>
											</div>
											<div class="box-body big">
												<h3 class="form-title">商品添加</h3>
												<form method="post" action="" class="form-horizontal" role="form" enctype="multipart/form-data">
												<input type="hidden" name="id" value="<?php echo $date['id']?>">
												  <div class="form-group">
													<label class="col-sm-3 control-label">商品名称</label>
													<div class="col-sm-2">
													  <input name="goods_name" value="<?php echo $date['goods_name']?>" class="form-control" type="text">
													</div>
												  </div>
												  <div class="form-group">
													<label class="col-sm-3 control-label">商品类型</label>
													<div style="width:auto;float:left" class="col-sm-2 pid">
													  <select name='cid' class="form-control" style="width:150px;float:left">
													         <?php foreach($cates as $v): ?>
													         <option <?php echo $v['id']==$date['cid']?'selected':''; ?> value="<?php echo $v['id']?>"><?php echo str_repeat('--',$v['lev']);echo $v['name']?></option>
													     <?php endforeach;?>
													  </select>
													</div>
												  </div>
												  <div class="form-group">
													<label class="col-sm-3 control-label">市场价</label>
													<div class="col-sm-2">
													  <input name="price" value="<?php echo $date['price']?>" class="form-control" type="text">
													</div>
												  </div>

		                                        <div class="form-group">
													 <label class="col-md-3 control-label">商品展示图片</label>
													 <div class="col-md-2">
														<div class="fileupload fileupload-new" data-provides="fileupload">
														<div class="input-group">
															<div class="form-control uneditable-input"><i class="fa fa-file fileupload-exists"></i>
																<span class="fileupload-preview"></span>
															</div>
															<div class="input-group-btn">
																<a class="btn btn-default btn-file">
																	<span class="fileupload-new">Select file</span>
																	<span class="fileupload-exists">Change</span>
																	<input  name="orignalimg" class="file-input" type="file">
																</a>
															</div>

														</div>
													    </div>
													 </div>
													 <?php if(!empty($date['thumbimg'])):?>
													 <img style="float:left" width="60" height="50" src="<?php echo $date['thumbimg']?>">
													 <?php else:?><span style="display:inline-block;margin-top:5px;">暂无展示图片</span>
													 <?php endif;?>
												</div>
												  <div class="form-group">
													<label class="col-sm-3 control-label">商品描述</label>
													<div class="col-sm-6">
													  <textarea rows="6" name="des" class="form-control"><?php echo $date['des']?></textarea>
													</div>
												  </div>

  												  <div class="form-group">
													<label class="col-sm-3 control-label">其它图片</label>
													<div class="col-sm-6">
                                                      <?php foreach($dates1 as $k=>$v): ?>
													  <div class="add" style="margin-bottom:10px;">
															<button imgid=<?php echo $v['id']; ?> style="float:left;line-height:10px;width:30px;height:20px;margin-right:10px;"><?php echo $k==0?'+':'-'; ?></button><input multiple="multiple" name="orignal_img[<?php echo $v['id']?>]" type="file">
															<img width="60" height="50" src="<?php echo $v['thumb_img']?>">
													  </div>
													  <?php endforeach;?>
													</div>
												  </div>







  												  <div class="form-group">
													<label class="col-sm-3 control-label">商品属性</label>
													<div class="col-sm-6">
													       <?php foreach($attr as $k=>$v):?>
													       <?php if($v['attr_type']==0): ?>
													       	 <?php if(!empty($goodsattr[$v['id']])):?>
                                                             <?php foreach($goodsattr[$v['id']] as $k2=>$v2):?>

															  <div class="form-group">
																<label class="col-sm-2 control-label"><a class="attr" href="javascript:void(0)">[<?php echo $k2==0?'+':'-';?>]</a>	<?php echo $v['attr_name']?></label>
																<div style="width:auto;float:left" class="col-sm-2 pid">
																  <select imgid='<?php echo $v2["id"]?>' aid='<?php echo $v["id"]?>' name='oldattr_id[<?php echo $v["id"]?>][<?php echo $v2["id"]?>]' class="form-control" style="width:150px;float:left;margin-right:10px;">
																         <?php
                                                                         $attrval=explode(',',$v['attr_value']);
																         foreach($attrval as $v1): ?>
																         <option <?php echo $v2['attr_value']==$v1?'selected':''; ?> value="<?php echo $v1?>"><?php echo $v1?></option>
																         <?php endforeach;?>
																  </select>
																  <input style="width:100px;" name="oldprices[]" class="form-control" value="<?php echo $v2['price']?>" placeholder="元" type="text">
																</div>
															  </div>
															     <?php endforeach;?>
															     <?php else:?>
															  <div class="form-group">
																<label class="col-sm-2 control-label"><a class="attr" href="javascript:void(0)">[+]</a><?php echo $v['attr_name']?></label>
																<div style="width:auto;float:left" class="col-sm-2 pid">
																  <select aid='<?php echo $v["id"]?>' name='attr_id[<?php echo $v["id"]?>][]' class="form-control" style="width:150px;float:left;margin-right:10px;">
																         <?php
                                                                         $attrval=explode(',',$v['attr_value']);
																         foreach($attrval as $v1): ?>
																         <option value="<?php echo $v1?>"><?php echo $v1?></option>
																     <?php endforeach;?>
																  </select>
																  <input style="width:100px;" name="prices[]" class="form-control" value="" placeholder="元" type="text">
																</div>
															  </div>
															     <?php endif;?>



                                                             <?php else:?>


                                                                    <?php if(!empty($goodsattr[$v['id']])):?>
															  <div class="form-group">
																<label class="col-sm-2 control-label"><?php echo $v['attr_name']?></label>
																<div class="col-sm-5">
																  <input name="oldattr_id[<?php echo $goodsattr[$v['id']][0]['id']?>]" class="form-control" value="<?php echo $goodsattr[$v['id']][0]['attr_value']?>" type="text">
																</div>
															  </div>

                                                                    <?php else:?>
															  <div class="form-group">
																<label class="col-sm-2 control-label"><?php echo $v['attr_name'];?></label>
																<div class="col-sm-5">
																  <input name="attr_id[<?php echo $v['id']?>]" class="form-control" value="" type="text">
																</div>
															  </div>
                                                                     <?php endif;?>


                                                            <?php  endif; ?>
															<?php endforeach;?>


													</div>
												  </div>


                                                <div class="col-sm-offset-3 col-sm-13">
                                                 <button type="submit" class="btn btn-success">提交</button>
													</div>
												 </form>
											</div>
										</div>



<script src="public/admin/js/jquery/jquery-2.0.3.min.js"></script>

<script type="text/javascript">
$(function(){
		//增加图片数量
		$('.form-group').on('click','button',function(){
			    if($(this).text()=='+'){
			        var clone=$(this).parent().clone();
			        clone.find('button').text('-');
			        clone.find('input').attr('name','new_img[]');//为新添加的input定义新的name名来判断是否上传新的图片
			        clone.find('button').attr('imgid','');
			        clone.find('img').remove();
			        $(this).parent().parent().append(clone);
			    }
			    else{

			    	// ajax删除其他图片,若是imgid没有值则表示是新添加的，就不执行ajax操作
                    var id=$(this).attr('imgid');
                    if(id!=''){
		                    $.ajax({
		                         type:'get',
		                         url:'index.php?c=goods&a=update&mod=admin&flag=delimg&&imgid='+id,
		                         success:function(res){
		                               if(res>0){
		                                   alert('删除成功');
		                               }
		                         }

		                    });
                    }

					$(this).parent().remove();
			    }
				return false;
		});


       // 增加单选属性操作
        $('.col-sm-6').on('click','.attr',function(){
        	    var obj=$(this);
			    if($(this).text()=='[+]'){
			        var clone=$(this).parent().parent().clone();
			        clone.find('.attr').text('[-]');
			        clone.find('input[name=price]').val('');
                    var aid=clone.find('select').attr('aid');

					clone.find('select').attr('name','attr_id['+aid+'][]');
					clone.find('select').attr('imgid','');
					clone.find('input').attr('name','prices[]');
			        $(this).parent().parent().after(clone);
			    }
			    else{
                    var imgid=$(this).parent().parent().find('select').attr('imgid');
                    if(imgid!=''){
		                    $.ajax({
		                          type:'get',
		                          url:'index.php?c=goods&a=update&flag=delattr&mod=admin&id='+imgid,
		                          success:function(res){
		                          	if(res>0){
		                          		  obj.parent().parent().remove();
		                                  alert('删除成功');

		                          	}

		                          }

		                    });
                    }else{
                    	obj.parent().parent().remove();
                    }

			    }
				return false;
        });








})
</script>